package com.jhh.pay.heli.manager;

import com.jhh.pay.heli.bean.Bank;
import com.jhh.pay.heli.bean.pay.OrderPayStateHto;
import com.jhh.pay.heli.bean.pay.PayForPersonHto;
import com.jhh.pay.heli.bean.sign.WithholdSign;
import com.jhh.pay.heli.bean.withhold.OrderWithholdStateHto;
import com.jhh.pay.heli.bean.withhold.WithholdHto;
import com.jhh.pay.heli.utils.Des3Encryption;
import com.jhh.pay.heli.utils.TextUtil;
import com.jhh.pay.heli.utils.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by zhangqi on 2017/12/4.
 */
@Component
public final class PayHelper {

    private static final Logger mLogger = LoggerFactory.getLogger(PayHelper.class);

    /**
     * 代笔代付参数
     * @param payForPerson
     * @return
     * @throws Exception
     */
    public static Map<String,String> toSign(PayForPersonHto payForPerson, String privateKey) throws Exception {

        LinkedHashMap<String,String> map = new LinkedHashMap<>();

        map.put("P1_bizType", payForPerson.getP1_bizType());
        map.put("P2_orderId", payForPerson.getP2_orderId());
        map.put("P3_customerNumber", payForPerson.getP3_customerNumber());
        map.put("P4_amount", payForPerson.getP4_amount());
        map.put("P5_bankCode", Bank.mBankMap.get(payForPerson.getP5_bankCode()));
        map.put("P6_bankAccountNo", payForPerson.getP6_bankAccountNo());
        map.put("P7_bankAccountName", payForPerson.getP7_bankAccountName());
        map.put("P8_biz", payForPerson.getP8_biz());
        map.put("P9_bankUnionCode",payForPerson.getP9_bankUnionCode());
        map.put("P10_feeType", payForPerson.getP10_feeType());
        map.put("P11_urgency", payForPerson.getP11_urgency());
        String p12_summary = payForPerson.getP12_summary();
        map.put("P12_summary", p12_summary==null || p12_summary.trim().equals("")?"无":p12_summary);

        for (Map.Entry<String, String> entry : map.entrySet()) {

            if(entry.getKey().equals("P9_bankUnionCode")){
                continue;
            }

            if(entry.getValue() == null || entry.getValue().trim().equals("")){
                mLogger.info(entry.getKey());
                throw new RuntimeException("检查一下参数");
            }
        }

        map.put("sign", Util.RSAsign(map,privateKey));
        return map;
    }

    /**
     * 单笔代付查询
     * @param orderStateHto
     * @return
     * @throws Exception
     */
    public static Map<String,String> toSign(OrderPayStateHto orderStateHto, String privateKey) throws Exception {

        LinkedHashMap<String,String> map = new LinkedHashMap<>();
        map.put("P1_bizType", orderStateHto.getP1_bizType());
        map.put("P2_orderId", orderStateHto.getP2_orderId());
        map.put("P3_customerNumber", orderStateHto.getP3_customerNumber());
        map.put("sign", Util.RSAsign(map,privateKey));
        return map;
    }

    /**
     * 单笔代扣
     * @param withholdHto
     * @return
     * @throws Exception
     */
    public static Map<String,String> toSign(WithholdHto withholdHto, WithholdSign withholdSign) throws Exception {

        LinkedHashMap<String,String> map = new LinkedHashMap<>();
        map.put("P1_bizType",withholdHto.getP1_bizType());
        map.put("P2_customerNumber",withholdHto.getP2_customerNumber());
        map.put("P3_orderId",withholdHto.getP3_orderId());
        map.put("P4_timestamp",withholdHto.getP4_timestamp());
        map.put("P5_payerName",withholdHto.getP5_payerName());
        map.put("P6_idCardType",withholdHto.getP6_idCardType());
        map.put("P7_idCardNo", Des3Encryption.encode(withholdSign.getDES3_KEY(),withholdHto.getP7_idCardNo()));
        map.put("P8_cardNo",Des3Encryption.encode(withholdSign.getDES3_KEY(),withholdHto.getP8_cardNo()));
        String p9_phone = withholdHto.getP9_phone();
        if(!TextUtil.isEmpty(p9_phone)){
            p9_phone = Des3Encryption.encode(withholdSign.getDES3_KEY(),p9_phone);
        }
        map.put("P9_phone",p9_phone);
        map.put("P10_currency",withholdHto.getP10_currency());
        map.put("P11_orderAmount",withholdHto.getP11_orderAmount());
        map.put("P12_goodsCatalog",withholdHto.getP12_goodsCatalog());
        map.put("P13_goodsName",withholdHto.getP13_goodsNam());
        map.put("P14_goodsDesc",withholdHto.getP14_goodsDesc());
        map.put("P15_terminalType",withholdHto.getP15_terminalType());
        map.put("P16_terminalId",withholdHto.getP16_terminalId());
        map.put("P17_orderIp",withholdHto.getP17_orderIp());
        map.put("P18_serverCallbackUrl",withholdHto.getP18_serverCallbackUrl());
        map.put("sign",Util.MD5sign(map,withholdSign.getMD5_KEY()));
        return map;
    }

    public static Map<String, String> toSign(OrderWithholdStateHto hto, WithholdSign withholdSign) throws Exception {
        LinkedHashMap<String,String> map = new LinkedHashMap<>();

        map.put("P1_bizType",hto.getP1_bizType());
        map.put("P2_customerNumber",hto.getP2_customerNumber());
        map.put("P3_orderId",hto.getP3_orderId());
        map.put("P4_timestamp",hto.getP4_timestamp());
        map.put("sign", Util.MD5sign(map,withholdSign.getMD5_KEY()));
        return map;
    }
}
